package oop.ex4.searchsamples;


import oop.ex4.search.*;

/**
 * @author Dima
 * This example implements board for well known "horse move" puzzle 
 * also known as "open knight tour"
 * http://en.wikipedia.org/wiki/Knight's_tour
 */
public class HorsePuzzle {

	/**
	 * This function checks parameters and executes the puzzle
	 * @param args 3 parameters - X,Y and timeout
	 */
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		long timeOut;
		int xSize,ySize;
	/* check valid number of args */
	if (args.length < 3) {
			System.out.println("Arguments should be X Y Time");
			return;
	}
                /* check validy of arguments, convert into properties */
	try {
		timeOut = Long.parseLong(args[2]);
		xSize = Integer.parseInt(args[0]);
		ySize = Integer.parseInt(args[1]);
	} catch (NumberFormatException e) {
		System.out.println("Invalid string inside arguments - should be X(int) Y(int) Time(long)");
		return;
	}
	if (xSize<=0 || ySize<=0 || timeOut<=1 || xSize>100 || ySize>100) {
		System.out.println("All numbers should be positive and reasonable");
		return;
	}

    /* start the puzzle  */
	// 1) Create board
	HorseBoard board = new HorseBoard(xSize, ySize);
	// 2) Create search
	DepthFirstSearch<HorseBoard,HorseMove> theSearch = new MyDepthFirstSearch<HorseBoard,HorseMove>();
	// 3) Search board for solution
	HorseBoard solution = theSearch.search(board, xSize*ySize, timeOut);
	// 4) Display solution
	System.out.println("Solution:\n" + solution);
	System.out.println("The quality of the obtained solution is "+solution.getQuality());
	System.out.println("This is "+(!solution.isBestSolution()?"not " :"")+"the best possible solution.");
	System.out.println(System.currentTimeMillis() - startTime);
 }
}
